package com.niuniu;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class SaoLeiExport {

    private static Map<Integer,Integer> map = new HashMap<>();
    private static int count = 1;
    public static void main(String[] args) {
        int num = 10000;
        for (int i = 0; i <num; i++) {
            RedBag redBag = new RedBag();
            grapRedbag(redBag);
        }
        System.out.println(map);
    }

    private static void grapRedbag(RedBag redBag){
        boolean isRay = redBag.getRedBagReward(false,false,50);

        redBag.getRedBagReward(true,true,50);
        redBag.getRedBagReward(true,true,50);
        redBag.getRedBagReward(true,true,50);
        redBag.getRedBagReward(true,true,50);
        redBag.getRedBagReward(true,true,50);

        if (isRay){
            count++;
        }else {
            map.putIfAbsent(count,0);
            map.put(count,map.get(count)+1);
            count=1;
        }
    }


    static class RedBag{
        //雷号
        private int rayNumber = getRandomNumber();
        //红包价格
        private int totalMoney = 1000;
        //当前剩余红包
        private int remainMoney = 1000;
        //剩余红包数
        private int remainSize = 10;
        //真实用户踩雷数量
        private int realRayCount;
        //是否是机器人
        private boolean isRobot=true;


        //获取红包的金额
        public boolean getRedBagReward(boolean playIsRobot,boolean victorySwitch,int victoryProb){
            int bonus;
            if (judgeIsVictory(victoryProb,victorySwitch,playIsRobot)){
                bonus = getVictoryRandomMoney(rayNumber);
            }else {
                bonus = getRandomMoney();
            }
            boolean whetherRay = whetherRay(bonus);
            //真实踩雷数++
            if ((!playIsRobot) && whetherRay) realRayCount++;
            return whetherRay;

        }

        private int getRandomMoney() {
            // remainSize 剩余的红包数量
            // remainMoney 剩余的钱
            if (remainSize == 1) {
                remainSize--;
                int sendMoney = (int) Math.round(remainMoney * 100) / 100;
                remainMoney = 0;
                return sendMoney;
            }
            Random r   = new Random();
            int min   = (int) (totalMoney*0.01); //
            //红包的最大额度是2*平均值 也就是在均值以上的概率是50 以下的概率是50 保证了公平性
            int max   = (int) (remainMoney / remainSize * 2);
            int money = (int) (r.nextDouble() * max);
            money = money <= min ? (int) (totalMoney*0.01): money;
            money = (int) (Math.floor(money * 100) / 100);
            remainSize--;
            remainMoney -= money;
            return money;
        }


        //是否踩雷
        private boolean whetherRay(int bonus){
            return bonus % 10 == rayNumber;
        }
        //是否踩雷
        private int getRandomNumber(){

            return (int) (Math.random()*10);
        }


        //判断是否必赢
        private boolean judgeIsVictory(int victoryProb,boolean victorySwitch,boolean playIsRobot)
        {
            int prob = (int) (Math.random()*100);
            //没有踩雷数
            return victorySwitch //开关已开启
                    && (victoryProb > prob) //触发必赢条件
                    && (!playIsRobot)//玩家不是机器人
                    && (this.isRobot)//庄家是机器人
                    && realRayCount == 0;
        }


        private int getVictoryRandomMoney(int rayNumber) {
            // remainSize 剩余的红包数量
            // remainMoney 剩余的钱
            if (remainSize == 1) {
                remainSize--;
                return (int) Math.round(remainMoney * 100) / 100;
            }
            Random r   = new Random();
            int min   = (int) (totalMoney*0.01); //
            //红包的最大额度是2*平均值 也就是在均值以上的概率是50 以下的概率是50 保证了公平性
            int max   = (int) (remainMoney / remainSize * 2);
            int money = (int) (r.nextDouble() * max);
            money = money <= min ? (int) (totalMoney*0.01): money;
            money = (int) (Math.floor(money * 100) / 100);
            remainSize--;

            int winMoney = money - money%10 + rayNumber;
            if (winMoney<min && (remainMoney-winMoney)<min){
                //赢的钱小于最小值 或者下一局的钱小于最小值 按正常处理
                remainMoney -= money;
                return money;
            }else {
                remainMoney -= winMoney;
                return winMoney;
            }
        }
    }
}
